home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
TAWUG
/
TAWUG Disk No. 56 (SHK)
/
TAWUG56.shk
/
BASIC.NOTES
(
.txt
)
< prev
next >
Wrap
AppleWorks Document
|
1987-07-01
|
13KB
|
286 lines
O=====|====|====|====|====|====|====|====|====|====|====|====|====|====|====|===
Page
PROGRAMMING IN APPLESOFT BASIC
David C. Hunt
I. Introduction
@BASIC is a language for computers. Compared with natural human H
Flanguages like English or French, it has a very small number of words E
Cand grammatical rules. BASIC stands for
eginners
ll-purpose G
ymbolic
nstruction
ode. It is not especially strong in any J
Hparticular area, but it does a good many things well. It is also quite J
Heasy to learn and uses words similar to English. BASIC is the language I
Gwhich comes included with nearly all micro-computers, however, not all I
Gversions are compatible. A program written in Applesoft BASIC may not 5
run in Commodore BASIC for example, and vice versa.
----------------------------------------------------
II. Starting BASIC
GApplesoft BASIC is built into the Apple IIe computer, however we would J
Hnot be able to save our programs without first loading a Disk Operating H
FSystem (DOS). To start working in BASIC with the computer off, first J
Hinsert the Apple System Master program disk (DOS 3.3), then turn on the E
CApple power switch. The disk drive light will go on. After a few J
Hmoments the light will go off, the drive will stop and your screen will
display the message:
BE SURE CAPS LOCK IS DOWN
CThe square bracket "]" is a
prompt
, meaning that the computer is I
Gwaiting for your instructions. The flashing rectangle is the
cursor
and indicates where the next character you type will appear.
----------------------------------------------------
III. Printing with BASIC
Now type:
PRINT "HELLO THERE"
HBe sure to include the quotation marks and press <Return>! If you make F
Da mistake you may use the left and right arrow keys to correct your K
Ityping BEFORE pressing <Return>. The <Return> key tells the computer to E
Caccept the line you just typed and act on it. The computer should J
Hrespond by printing your message. Anything included in quotation marks K
Ifollowing the command PRINT will be printed literally on the screen. If K
Iyou mispel PRINT you will get the error message "?SYNTAX ERROR". If you K
Iomit the quotation marks the computer will think you are refering to the ?
name of a variable (more on that later) and PRINT a zero (0).
Try some arithmetic:
PRINT 5 + 5
PRINT 321-123
PRINT 4/2
PRINT 4*2
IThe computer uses the * to indicate multiplication and the / to indicate
division.
Let's get rid of all this garbage. Now type:
We can imbed punctuation marks to control the format of printing. Try:
PRINT "HELLO";"THERE"
then,
PRINT "HELLO","THERE"
AThe semicolon (;) tells the computer to hold the cursor position K
Iimmediately after printing. That is why there is no space between words J
Hin 'HELLOTHERE'. The comma (,) tells the computer to print at the next L
J"tab zone". It will space over to a preset tab distance and then print. J
HThat is why there are so many spaces between "Hello" and "There" in the J
Hsecond example. Note that if these characters were included within the J
quotation marks they would be printed "as is" and have no format effect.
----------------------------------------------------
IV. Programs
Type the command: NEW.
Type the command: RUN.
HThe computer responds by doing nothing. It has no program in memory to 3
RUN. We can store a set of commands in programs.
10 PRINT "HELLO THERE"
20 PRINT "BYE NOW"
30 END
DIf you make a mistake, retype the entire line. Some computers will H
Fallow you to move anywhere on the screen with the arrow keys and make 5
corrections. The Apple does not have this feature.
HBe sure to use line numbers. This is how the computer knows that these H
Fcommands will be executed later and not immediately after being typed J
Hin. We have numbered by tens so that we will have room in case we need F
Dto make changes and add other lines in between later. Now run this !
little program by typing: RUN
This time BASIC responds:
HELLO THERE
BYE NOW
We can have this program run itself several times successively. Try:
5 REM MY FIRST PROGRAM
30 GOTO 10
40 END
Now type the command: LIST
IThe computer will respond by listing your program on the screen. Notice G
Ethat the new line #30 has replaced the old and lines #10 and 40 have J
Hbeen added. The lines are also listed in correct numerical order. The J
HREM statement in line 10 simply means REMark. It is a place for you to K
Imake comments for yourself. The computer will ignore any line beginning
with REM.
Now type: RUN
HThe program will print the statements in lines 10 and 20. Then line 30 H
Ftells it to go back to line 10 and it becomes an "infinite loop". Do I
Gyou see why the program will never reach line 40? If you wish to stop J
Hthis procedure press CTRL-C. You do this by holding down the <CONTROL> (
key and pressing <C> at the same time.
----------------------------------------------------
V. Saving and Loading
If your own disk is in the drive, you can save your programs.
Type: SAVE PROGRAM1
>The most recent program in memory is now saved under the name
"PROGRAM1".
To see what is on your disk type: CATALOG
GIf you see that your program has been saved, then clear the computer's
memory by typing: NEW
Now LIST. There should be nothing to LIST in memory.
FAt some later time you can then retrieve these programs from disk and "
use them again without retyping.
Type: LOAD PROGRAM1
GWhen the disk drive light goes out type LIST. Your program is back in
memory again!
VI. Variables
HVariables are like little mailboxes in the computer's memory. Each box E
Ccan hold a piece of information you put in it. The mailboxes have J
Hrather strange names rather than numbers. The name indicates what kind !
of information is stored there.
IFor example, we might have a value stored in a mailbox, or a "string" of A
letters, numbers and other characters. We can do this like so:
A = 1
A$ = "HELLO THERE!"
To prove that it worked type: PRINT A
Then try: PRINT A$
FThe contents of those variables is printed. You do NOT use quotation &
marks around the name of a variable!
Try: PRINT B
There is nothing stored in mailbox "B" now so the result printed is "0".
CA$ is pronounced "A-string". The dollar sign ($) indicates that a <
"string of characters" rather than a value is stored here.
Variables can also be assigned within a program.
Be sure to type NEW to clear any old programs from memory! Then:
10 A$="ONE"
20 B$="TWO"
30 C$="THREE"
40 A=1
50 B=2
60 C=3
70 PRINT A$,A
80 PRINT B$,B
90 PRINT C$,C
100 END
HTry to figure out what this program will print on the screen. Then RUN
----------------------------------------------------
VII. READ-DATA
HThere is another way to put information into variables. We can use the #
pair of statements READ and DATA.
HType NEW to clear the previous program from memory. Type HOME to get a
clean screen to work with.
10 READ A$,B$,C$,A,B,C
20 DATA ONE,TWO,THREE,1,2,3)
30 PRINT A$,A : PRINT B$,B : PRINT C$,C
40 END
JThis program will have the same effect as the last, but it much shorter. I
GIt is a better use of memory. Notice how the colon (:) can be used to J
Hseparate statements and allow them to be combined on one line. Not all
versions of BASIC allow this.
----------------------------------------------------
VIII. FOR-NEXT
DWe can create a progaram that will execute a given range of lines a G
Especific number of times. We do this with the pair FOR and NEXT. A I
Gvariable is used as a "counter" in the FOR line to keep track of where
we are.
D10 PRINT "MY NAME IS .................":REM--INCLUDE YOUR NAME HERE
INSTEAD OF THE DOTS!
20 END
RUN the program. Then add the following lines.
5 FOR X = 1 TO 10 STEP 1
15 NEXT X
LIST the program to be sure it is all there.
GNow the program will assign the variable "X" the value "1" and execute I
Gline 10. At line 15 the program will branch back to line 5. Variable G
E"X" will have 1 added to it (the STEP) and have the value "2". This G
Econtinues until the value of the variable is 11. When that point is D
Breached in line 5 it no longer is in the range 1-10 so control is H
transferred past the NEXT statement to line 20 where the program ENDs.
Try retyping line 5 and increasing the range, say X=1 TO 100.
----------------------------------------------------
IX. INPUT
FMicro-computers often are used in an "interactive" environment. That F
Dis, the user will enter information directly when asked. The INPUT C
statement tells the computer to accept information from the user.
Clear memory with the NEW command.
10 INPUT NA$
20 PRINT NA$
30 END
@When this is RUN, a question mark "?" prompt will appear. This I
Gindicates that the computer is waiting for some information from you. J
HIt will wait until you press <Return>. That information will be stored G
Ein the variable NA$. INPUT will not allow the use of commas (,). A ?
comma will give you the unfriendly response "?EXTRA IGNORED".
Add the following for greater clarity:
5 PRINT "PLEASE ENTER YOUR NAME";
FA prompt line may be included in the request for input. Clear memory
before enterring this program:
10 HOME#
20 INPUT "WHAT IS YOUR NAME?";NA$
30 PRINT
40 PRINT "HELLO, ";NA$
50 END
INote that line 30 does nothing more than print a blank space to increase ?
readability. Line 10 clears the screen for the same purpose.
----------------------------------------------------
X. IF-THEN
HA computer can appear to make decisions or posses intelligence by using I
Gthe IF and THEN commands. Insert YOUR NAME in place of the dots (...)
in this example.
10 HOME#
20 PRINT "HALT! WHO GOES THERE?"
30 INPUT NA$
40 PRINT=
50 IF NA$ = ".........." THEN PRINT "WELCOME, ",NA$:GOTO 70H
60 IF NA$ <> ".........." THEN PRINT "I DON'T KNOW YOU. GOODBYE.":END*
70 PRINT: PRINT "LET'S PLAY SOME GAMES!"/
80 REM THE REST OF THE PROGRAM WOULD GO HERE
90 END
INow when the program gets to line 50-60 it will check to see if the name D
Byou gave at the INPUT prompt matches the name it is programmed to I
Gaccept. It so (line 50), then it will greet you and skip ahead to the I
Grest of the program. If the names differ it drops through to line 60, '
prints the Goodbye message and quits.
HLet's add a few more lines to this same program, again using INPUT. Do
NOT type NEW.
80 PRINT "ENTER 'C' TO CONTINUE"
90 INPUT C$
100 IF C$ = "C" THEN GOTO 10
110 IF C$ <> "C" THEN END
DIn this case by entering the letter "C" (followed by <Return>) when J
Hasked the program will start over from the beginning. Any other letter H
Fwill end the program. Try this program several times. Use your name "
first, then try some other name.
----------------------------------------------------
On Your Own
10 REM THIS PROGRAM WILL PRINT A UNIQUE VERSION OF HUMPTY DUMPTY
20 PRINT:PRINT
30 PRINT "ENTER A VERB:"
40 INPUT A$
50 PRINT "ENTER A NOUN"
60 INPUT B$
70 PRINT "ENTER AN ADJECTIVE:"
80 INPUT C$
90 PRINT "ENTER A NOUN"
1O0 INPUT D$"
110 PRINT "ENTER A PLURAL NOUN:"
120 INPUT E$"
130 PRINT "ENTER A PLURAL NOUN:"
140 INPUT F$
150 PRINT "ENTER A VERB:"
160 INPUT G$
170 PRINT:PRINT:PRINT:PRINT,
180 PRINT "A NEW VERSION OF HUMPTY DUMPTY"
190 PRINT:PRINT+
200 PRINT "HUMPTY DUMPTY ";A$;" ON A ";B$,
210 PRINT "HUMPTY DUMPTY HAD A ";C$;" ";D$:
220 PRINT "ALL THE KING'S ";E$;" AND ALL THE KING'S ";F$4
230 PRINT "COULDN'T ";G$;" HUMPTY TOGETHER AGAIN."
240 END
10 PRINT "THIS SAMPLE PROGRAM WILL ILLUSTRATE HOW A COMPUTER CAN<
15 PRINT "BE USED TO ASK QUESTIONS AND RESPOND TO ANSWERS"
20 PRINT:PRINT
30 PRINT "WHAT IS YOUR NAME";
40 INPUT N$
50 PRINT:PRINTE
60 PRINT "WHO WAS THE FIRST PRESIDENT OF THE U.S. (LAST NAME ONLY)"
70 INPUT B$
80 PRINT:PRINTD
90 IF B$="WASHINGTON" THEN PRINT "GOOD SHOW ";N$;"-THAT'S CORRECT"B
100 IF B$<>"WASHINGTON" THEN PRINT "TOO BAD ";N$;"-THAT'S WRONG"
110 END
10 PRINT "THIS PROGRAM WILL FIND THE AREA AND PERIMETER OF A SQUARE"
20 PRINT:PRINT5
30 PRINT "ENTER THE LENGTH OF THE SIDE OF A SQUARE"
35 INPUT S
40 LET A=S^2
50 LET P=4*S
60 PRINT:PRINT:PRINT:PRINT)
70 PRINT "THE AREA OF THE SQUARE IS ";A
80 PRINT.
90 PRINT "THE PERIMETER OF THE SQUARE IS ";P
100 END